home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_02 / ED-157 / get_token.c < prev    next >
C/C++ Source or Header  |  1993-09-14  |  2KB  |  73 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record (rhr@clio.rice.edu)
  3.  * 
  4.  * This file is part of ED.
  5.  * 
  6.  * ED is free software; you can redistribute it and/or modify it under the terms
  7.  * of the GNU General Public License as published by the Free Software Foundation.
  8.  * 
  9.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License along with ED
  14.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  15.  * Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17. #include "opsys.h"
  18.  
  19. #include <string.h>
  20.  
  21. #include "ctyp_dec.h"
  22.  
  23. /******************************************************************************\
  24. |Routine: get_token
  25. |Callby: load_file parse_fnm
  26. |Purpose: Gets the next token from a string. Returns 0 if no more tokens remain.
  27. |Arguments:
  28. |    buf is the string containing tokens. A token is deleted from the string
  29. |            each time the routine is called.
  30. |    tok is the returned token.
  31. \******************************************************************************/
  32. Int get_token(buf,tok)
  33. Char *buf,*tok;
  34. {
  35.     register Char *s,*e;
  36.     Char *from,*to;
  37.  
  38.     if(!strlen(buf))
  39.         return(0);
  40.     s = buf;
  41.     while(isspace(*s++));
  42.     if(!*--s)
  43.         return(0);
  44.     e = s;
  45.     if(*e == '"')    /* handle quoting */
  46.     {
  47.         e++;
  48.         while(1)
  49.         {
  50.             if(*e == '"')
  51.                 if(*(e - 1) != '\\')
  52.                     break;
  53.             e++;
  54.         }
  55.         e++;
  56.     }
  57.     else    /* not a quoted string */
  58.     {
  59.         while(!isspace(*++e))
  60.             if(!*e)
  61.             {
  62.                 *(e + 1) = 0;    /* this could be dangerous */
  63.                 break;
  64.             }
  65.     }
  66.     *e = 0;
  67.     strcpy(tok,s);
  68.     *e = ' ';
  69.     for(from = e,to = buf;(*to++ = *from++););
  70.     return(1);
  71. }
  72.  
  73.